package com.example.himalayaproject.find.fenlei.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.fenlei.Callback.HttpService;
import com.example.himalayaproject.find.fenlei.activity.FenLei_GuangGao_ViewPager_xiangqing_activity;
import com.example.himalayaproject.find.fenlei.adatper.Fenlei_RecyclerViewPagerAdapter;
import com.example.himalayaproject.find.fenlei.adatper.Fenlei_ViewPager_Adapter;
import com.example.himalayaproject.find.fenlei.bean.FenLei_RootBean;
import com.example.himalayaproject.find.fenlei.bean.ViewPagerBean;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class FenLei_Fragment extends Fragment implements Callback<FenLei_RootBean>, Fenlei_RecyclerViewPagerAdapter.OnChildClickListener {

    private RecyclerView recyclerView;
    private Fenlei_RecyclerViewPagerAdapter adapter;
    private List<FenLei_RootBean.Fenlei_Data> list;
    private List<FenLei_RootBean.Data> dataList;
    private ImageView imageView;
    private ViewPager viewPager;
    private List<ImageView> imageViewList;
    private int curPosition = 1;

    public FenLei_Fragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_fen_lei_, container, false);
//        imageView = (ImageView) rootView.findViewById(R.id.fragment_fenlei_imageview);
        viewPager = (ViewPager) rootView.findViewById(R.id.fragment_fenlei_viewpager);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.fragment_fenlei_recyclerview);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service = retrofit.create(HttpService.class);
        service.getText().enqueue(this);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) {
                    return 2;
                }
                return 1;
            }
        });
        recyclerView.setLayoutManager(manager);
        adapter = new Fenlei_RecyclerViewPagerAdapter(getContext());
        recyclerView.setAdapter(adapter);
        adapter.setListener(this);
        RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildAdapterPosition(view) % 6 == 0) {
                    outRect.set(0, 0, 0, 20);
                }
            }
        };
        recyclerView.addItemDecoration(decoration);
        Retrofit retrofit1 = new Retrofit.Builder()
                .baseUrl("http://adse.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service1 = retrofit1.create(HttpService.class);
        service1.getText1().enqueue(new Callback<ViewPagerBean>() {
            @Override
            public void onResponse(Call<ViewPagerBean> call, final Response<ViewPagerBean> response) {
                final List<ImageView> imageViewList = new ArrayList<>();
                ImageView imageView1 = new ImageView(getContext());
                Picasso.with(getContext()).load(response.body().getData().get(2).getCover()).into(imageView1);
                imageViewList.add(imageView1);
                ImageView imageView2 = new ImageView(getContext());
                Picasso.with(getContext()).load(response.body().getData().get(0).getCover()).into(imageView2);
                imageViewList.add(imageView2);
                ImageView imageView3 = new ImageView(getContext());
                Picasso.with(getContext()).load(response.body().getData().get(1).getCover()).into(imageView3);
                imageViewList.add(imageView3);
                ImageView imageView4 = new ImageView(getContext());
                Picasso.with(getContext()).load(response.body().getData().get(2).getCover()).into(imageView4);
                imageViewList.add(imageView4);
                ImageView imageView5 = new ImageView(getContext());
                Picasso.with(getContext()).load(response.body().getData().get(0).getCover()).into(imageView5);
                imageViewList.add(imageView5);
                viewPager.setAdapter(new Fenlei_ViewPager_Adapter(imageViewList));
                imageView2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getContext(), FenLei_GuangGao_ViewPager_xiangqing_activity.class);
                        intent.putExtra("url", response.body().getData().get(0).getLink());
                        getContext().startActivity(intent);
                    }
                });
                imageView3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getContext(), FenLei_GuangGao_ViewPager_xiangqing_activity.class);
                        intent.putExtra("url", response.body().getData().get(1).getLink());
                        getContext().startActivity(intent);
                    }
                });
                imageView4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getContext(), FenLei_GuangGao_ViewPager_xiangqing_activity.class);
                        intent.putExtra("url", response.body().getData().get(2).getLink());
                        getContext().startActivity(intent);
                    }
                });
                viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        curPosition = position;
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                        switch (state) {
                            case ViewPager.SCROLL_STATE_IDLE:

                                if (curPosition == 0) {
                                    viewPager.setCurrentItem(imageViewList.size() - 2, false);
                                } else if (curPosition == imageViewList.size() - 1) {
                                    viewPager.setCurrentItem(1, false);
                                }
                                break;
                            case ViewPager.SCROLL_STATE_DRAGGING:
                                Log.d("1620", "dragging");
                                break;
                            case ViewPager.SCROLL_STATE_SETTLING:
                                Log.d("1620", "settling");
                                break;
                        }
                    }
                });
                viewPager.setCurrentItem(1, false);
            }

            @Override
            public void onFailure(Call<ViewPagerBean> call, Throwable t) {

            }
        });
    }


    @Override
    public void onResponse(Call<FenLei_RootBean> call, Response<FenLei_RootBean> response) {
        list = response.body().getList();
        adapter.addAll(list);
    }

    @Override
    public void onFailure(Call<FenLei_RootBean> call, Throwable t) {

    }

    @Override
    public void onChildClick(int position, long id, FenLei_RootBean.Fenlei_Data data) {
        adapter.start(position);
    }
}
